﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class ScheduleDAL
    {
        private static Schedule CreateScheduleFromReader(SqlDataReader reader)
        {
            Schedule schedule = new Schedule
            {
                No = Utility.StringParse(reader[ScheduleColumns.No.ToString()]),
                ChannelID = Utility.StringParse(reader[ScheduleColumns.ChannelID.ToString()]),
                ChannelName = Utility.StringParse(reader[ChannelColumns.ChannelName.ToString()]),
                Type = Utility.StringParse(reader[ScheduleColumns.Type.ToString()]),
            };

            schedule.Channel = new Channel
            {
                ChannelID = schedule.ChannelID,
                ChannelName = Utility.StringParse(reader[ChannelColumns.ChannelName.ToString()]),
            };

            return schedule;
        }

        public static List<Schedule> GetScheduleList()
        {
            try
            {
                List<Schedule> scheduleList = new List<Schedule>();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ScheduleGetList))
                {
                    while (reader.Read())
                    {
                        scheduleList.Add(CreateScheduleFromReader(reader));
                    }
                }

                return scheduleList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetScheduleList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SCHEDULE_GETLIST);
            }
        }
    }
}